<?php
/**
 * Created by PhpStorm.
 * User: serenehaly
 * Date: 2018/2/5
 * Time: 11:17
 */

namespace app\api\controller\v1;


use app\api\validate\Count;
use app\api\model\Product as ProductModel;
use app\api\validate\PositiveInteger;
use app\lib\exception\ProductException;

class Product
{
    public function getMostRecend($count = 15)
    {
        $validate = new Count();
        $validate->goCheck();
        $product = ProductModel::getRecend($count);
        if ($product->isEmpty()) {
            throw new ProductException();
        } else {
            $product = $product->hidden(['summary']);
            return $product;
        }
    }

    public function getAllByCategoryID($id)
    {
        $validate = new PositiveInteger();
        $validate->goCheck();
        $product = ProductModel::getAllByCategoryID($id);
        if ($product->isEmpty()) {
            throw new ProductException();
        } else {
            $product = $product->hidden(['summary']);
            return $product;
        }
    }

    public function getOne($id)
    {
        $validate = new PositiveInteger();
        $validate->goCheck();
        $product = ProductModel::getProductByID($id);
        if (!$product) {
            throw new ProductException();
        } else {
            return $product;
        }
    }
}